package edu.westga.sketchit2009cs.stringBufferServer;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

class ClientWorker implements Runnable {
	  private Socket client;

	  //Constructor
	  public ClientWorker(Socket client) {
		  this.client = client;
	  }

	  public void run() {
		  
		  String line;
		  BufferedReader in = null;
		  PrintWriter out = null;
		  
		  try {
			  in = new BufferedReader(new InputStreamReader(client.getInputStream()));
			  out = new PrintWriter(client.getOutputStream(), true);
		  } catch (Exception e) {
			  System.out.println("in or out failed");;
		  }
		  
	      try {
	    	  
	    	  line = in.readLine();
	    	  
	    	  if(line.endsWith("incoming")) {
	    		  DrawingServer.theBuffer.setValue(line);
	    	  } else {
	    		  if(line.equals("request")){
	    			  out.println(DrawingServer.theBuffer.getValue());
	    		  }
	    	  }
	    	  this.client.close();
	      } catch (Exception e) {
	    	  System.out.println("Read or write failure");
	    	  e.printStackTrace();
	      }
	}
	  
}

